//
//  MNDBindBankcardViewController.m
//  minidai
//
//  Created by Steven.Yung on 2017/5/10.
//  Copyright © 2017年 迷你金服. All rights reserved.
//

#import "MNDBindBankcardViewController.h"
#import "MNDBindBankcardModel.h"
#import "NSString+ReplaceAsterisk.h"
#import "UIImage+JKColor.h"
#import "NSString+JKNormalRegex.h"
#import "UIButton+JKCountDown.h"

@interface MNDBindBankcardViewController ()

@property (weak, nonatomic) IBOutlet UITextField *usernameTextField;
@property (weak, nonatomic) IBOutlet UITextField *userBankcardTextField;
@property (weak, nonatomic) IBOutlet UITextField *userPhoneTextField;
@property (weak, nonatomic) IBOutlet UITextField *userCaptchaTextField;
@property (weak, nonatomic) IBOutlet UIButton *submitButton;

@property (strong, nonatomic) MNDBindBankcardModel *bankcardModel;


@end

@implementation MNDBindBankcardViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.bankcardModel = [MNDBindBankcardModel new];
    self.bankcardModel.channel = @"app";
    self.bankcardModel.userRealname = self.bankModel.userAmount.userRealname;
    self.bankcardModel.phoneNum = self.bankModel.jxBankaccount.userAmountMobile;
    
    [self.submitButton setBackgroundImage:[UIImage imageNamed:@"invest_result_continue_btn"] forState:UIControlStateNormal];
    [self.submitButton setBackgroundImage:[UIImage jk_imageWithColor:kColorLightGray] forState:UIControlStateDisabled];
    
    self.usernameTextField.text = self.bankModel.userAmount.userRealname;

    RAC(self.bankcardModel, phoneCode) = self.userCaptchaTextField.rac_textSignal;
    RAC(self.bankcardModel, cardno) = self.userBankcardTextField.rac_textSignal;
    
    RAC(self.submitButton, enabled) = [RACSignal combineLatest:@[RACObserve(self.bankcardModel, cardno),
                                                                  RACObserve(self.bankcardModel, phoneCode)]
                                                         reduce:^id(NSString *carNumber,
                                                                    NSString *captcha){
                                                             
                                                             return @(carNumber.length > 0 && captcha > 0);
                                                         }];
    WeakSelf(self);
    [self.userBankcardTextField.rac_textSignal subscribeNext:^(NSString *text) {
        
        if (text.length > 19) {
            weakself.userBankcardTextField.text = [text substringToIndex:19];
        }
    }];
    
    __block UITextField *phoneTextField = self.userPhoneTextField;
    [self.userPhoneTextField.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
        if (x.length > 11) {
            phoneTextField.text = [x substringToIndex:11];
        }
    }];
    
    __block UITextField *captchaTextField = self.userCaptchaTextField;
    [self.userCaptchaTextField.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
        if (x.length > 6) {
            captchaTextField.text = [x substringToIndex:6];
        }
    }];
    
    self.userPhoneTextField.text = self.bankModel.jxBankaccount.userAmountMobile;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

#pragma mark - Network

- (void)popToPreVC {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)sendPhoneCaptcha:(UIButton *)sender {
    [NetworkProgressHUD showHud];
    NSDictionary *params = @{@"cardno": self.userBankcardTextField.text, @"channel": @"app"};
    
    [APIManager sendRequestWithPath:API_BIND_CARD_SEND_CAPTCHA parameters:[MNDSign signedParamWithDict:params] method:kXMHTTPMethodPOST onSuccess:^(ResponseModel *responseModel) {
        if (responseModel.status == ResponseSuccess) {
            [NetworkProgressHUD showHudMsg:@"验证码已发送"];
            [sender jk_startTime:30 title:@"重新获取" waitTittle:@"秒后重发"];
            
        } else {
            NSDictionary *dic = responseModel.msg;
            [NetworkProgressHUD showHudMsg:dic[@"error"]];
        }
    } onFailure:^(NSError *error) {
        [NetworkProgressHUD showHudNetworkError:error];
    }];
}

- (void)bindBankcard {
    
    WeakSelf(self);
    [NetworkProgressHUD showHud];
    NSLog(@"params: %@", self.bankcardModel.mj_JSONObject);
    NSDictionary *params = [MNDSign signedParamWithDict:self.bankcardModel.mj_JSONObject];
    
    [APIManager sendRequestWithPath:API_BIND_BANKCAR parameters:params method:kXMHTTPMethodPOST onSuccess:^(ResponseModel *responseModel) {
        if (responseModel.status == ResponseSuccess) {
            [NetworkProgressHUD showHudMsg:@"绑卡成功"];
            [weakself performSelector:@selector(popToPreVC) withObject:nil afterDelay:2.0];
        } else {
            NSDictionary *dict = responseModel.msg;
            if (dict && dict.count > 0) {
                [NetworkProgressHUD showHudMsg:dict[@"error"]];
                
            } else {
                [NetworkProgressHUD showHudMsg:@"获取数据失败"];
            }
        }
    } onFailure:^(NSError *error) {
        [NetworkProgressHUD showHudNetworkError:error];
    }];
}

#pragma mark - Action

- (IBAction)sendPhoneCaptchaAction:(id)sender {
    [self.view endEditing:YES];
    [self sendPhoneCaptcha:sender];
}

- (IBAction)addBankcardAction:(id)sender {
    [self.view endEditing:YES];
    if (self.bankcardModel.cardno.length <= 0) {
        [NetworkProgressHUD showHudMsg:@"请输入银行卡号"];
        return;
    }
    
    if (self.bankcardModel.phoneCode.length <= 0) {
        [NetworkProgressHUD showHudMsg:@"请输入验证码"];
        return;
    }
    
    [self bindBankcard];
}

@end
